package com.faker.controller;

import com.faker.core.dto.ResponseResult;
import com.faker.core.jdbc.DataSourceChange;
import com.faker.service.ConnectionManagerService;
import com.faker.service.ConnectionOperatorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 连接操作 控制器
 */
@RestController
@RequestMapping(value = "connectionOperator")
@Slf4j
public class ConnectionOperatorController {
    @Resource
    private ConnectionOperatorService connectionOperatorService;


    /**
     * 获取表名列表
     *
     * @param connectionId
     * @return
     */
    @GetMapping(value = "/listTable")
    public ResponseResult listTable(@RequestParam(name = "connectionId") Long connectionId) {
        DataSourceChange.changeDataSource(connectionId);
        List<Map<String, Object>> tableList = connectionOperatorService.getTableList(connectionId);
        return ResponseResult.success(tableList);

    }


    /**
     * 获取表字段列表
     *
     * @param connectionId
     * @param tableName
     * @return
     */
    @GetMapping(value = "/listField")
    public ResponseResult listField(@RequestParam(name = "connectionId") Long connectionId, @RequestParam(name = "tableName") String tableName) {
        DataSourceChange.changeDataSource(connectionId);
        List<Map<String, Object>> fieldList = connectionOperatorService.getFieldList(tableName);
        return ResponseResult.success(fieldList);
    }


}
